package com.leige.utils.tool;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 操作上下文
 * @author Administrator
 *
 */
@Component
public final class Context {
	private final static ThreadLocal<Context> contextHolder = new ThreadLocal<Context>(){@Override protected Context initialValue() { return new Context(); }};

	private HttpServletRequest request;

	private Map<String, Object> contextMap=new HashMap<>();
	
	private HttpServletResponse response;
	
	private static Context getContext(){
		return contextHolder.get();
	}
	
	public static void remove(){
		String title=getTitle();
		String message=getMessage();
		contextHolder.remove();
	}
	/**
	 * 当前登录的用户，如果存在(线程用户 )
	 */
	private Object 	user;
	
	/**
	 * 当前线程中得消息
	 */
	private String 	message;
	
	/**
	 * 当前操作的标题
	 */
	private String 	title;

	public static Object getUser() {
		if(getContext().user!=null)
			return getContext().user;
		Object user=new Object();
			return user;
	}

	public static void setUser(Object user) {
		getContext().user = user;
	}

	public static String getMessage() {
		return getContext().message;
	}

	public static void setMessage(String message) {
		getContext().message = message;
	}

	public static String getTitle() {
		return getContext().title;
	}

	public static void setTitle(String titleformat,Object ... arg1) {
		getContext().title = String.format(titleformat, arg1);
	}

	public static void setRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		getContext().request = request;
	}

	public static void setResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		getContext().response = response;
	}
	public static HttpServletRequest getRequest() {
		return getContext().request;
	}
	public static HttpSession getSession() {
		HttpServletRequest request=getRequest();
		if(request==null)
			return null;
		return request.getSession();
	}
	public static HttpServletResponse getResponse() {
		return getContext().response;
	}
	/**
	 * 设置值全部设置的request
	 * @param key
	 * @param valoue
	 */
	public static void put(String key,Object valoue){
		Context context=getContext();
		if(context.request!=null)
			context.request.setAttribute(key, valoue);
		else
			context.contextMap.put(key, valoue);
	}
	
	/**
	 * 首先从request中取，取不到从请求参数中取，继续道请求头中取，继续到会话中取，继续到ServletContext中取,继续到redis中取,如果依然取不到，就返回null
	 * @param key
	 * @return
	 */
	public static Object get(String key){
		Context context=getContext();
		if(context.request==null)
			return context.contextMap.get(key);
		Object valoue=context.request.getAttribute(key);
		if(valoue!=null)
			return valoue;
		valoue=context.request.getParameter(key);
		if(valoue!=null)
			return valoue;
		valoue=context.request.getHeader(key);
		if(valoue!=null)
			return valoue;
		HttpSession session = context.request.getSession();
		valoue=session.getAttribute(key);
		if(valoue!=null)
			return valoue;
		valoue=session.getServletContext().getAttribute(key);
		if(valoue!=null)
			return valoue;
		return context.contextMap.get(key);
	}
	public static void addCookie(String name,String value){
		HttpServletResponse response=getResponse();
		Cookie cookie=new Cookie(name, value);
		response.addCookie(cookie);
	}
	public static void addCookie(String name,String value,String domain){
		HttpServletResponse response=getResponse();
		Cookie cookie=new Cookie(name, value);
		cookie.setDomain(domain);
		response.addCookie(cookie);
	}

	public static void delCookie(String name){
		if(name==null)return;
		HttpServletResponse response=getResponse();
		HttpServletRequest request=getRequest();
		if(response==null||request==null)return;
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			if(name.equals(cookie.getName()))
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
	}
}
